<template>
  <!-- 表单项管理 -->
  <div class="centent">
    <div style="background-color: #FFFFFF;padding: 15px;">
      <div class="ipt_box">
        <div>
          <span style="margin-right: 5%;">表单项名称:</span> <el-input v-model="input" placeholder="请输入名称" clearable style="width: 40%;" />
          <el-button style="margin-left: 2%;" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </div>
        <div>
          <el-button style="margin-left: 5%;" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
        </div>

      </div>
      <el-table :data="tableData" border style="width: 100%">

        <el-table-column prop="formIndexName" label="名称" />

        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="indexName" label="关联表单项" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="createTime" label="是否设置为自定义加分项">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isCustomeAdd==1?true:false"
              active-color="#13ce66"
              inactive-color="#cccccc"
              @change="customeAdd(scope.row)"
            />
          </template>
        </el-table-column>

        <el-table-column prop="createTime" label="是否设置为自定义减项">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isCustomeDelete==1?true:false"
              active-color="#13ce66"
              inactive-color="#cccccc"
              @change="customeDelete(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="type" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" round @click="goedit(scope.row, scope.$index)">编辑</el-button>
            <el-button size="mini" type="success" round @click="guanlian(scope.row, scope.$index)">关联</el-button>
            <el-button size="mini" type="danger" round @click="removeTodo(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-dialog title="关联指标项" :visible.sync="dialogTableVisible">
        <el-form :model="form">

          <el-form-item label="指标项" label-width="auto">
            <el-select v-model="form.region" style="width: 100%;" filterable placeholder="请选择指标项">
              <el-option v-for="(item,index) in options" :key="item.value" :label="item.label" :value="item.value" />

            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogTableVisible = false">取 消</el-button>
          <el-button type="primary" @click="isok">确 定</el-button>
        </div>
      </el-dialog>
      <div style="margin-left: 50%;margin-top: 10px;">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[ 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalcount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogTableVisible: false,
      form: {
        region: ''
      },
      options: [],
      //  列表数据源
      tableData: [],
      // 当前分页位置
      currentPage: 1,

      pageSize: 10,
      totalcount: 0,
      // 搜索框
      input: '',
      // 关联
      guanlianid: null
    }
  },
  created() {
    this.getlist()
    this.getxialalist()
  },
  methods: {
    customeAdd(data) {
      console.log(data)

      this.$Http.setCustomAddFormIndex({ id: data.id }).then(res => {
        if (res.code === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getlist()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    customeDelete(data) {
      console.log(data)
      this.$Http.setCustomDeleteFormIndex({ id: data.id }).then(res => {
        if (res.code === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getlist()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    // 新增表单项
    add() {
      this.$router.push('/editform')
    },
    // 关联指标项确定
    isok() {
      this.$Http.guanlitijiao({ assessmentIndexId: this.form.region, id: this.guanlianid }).then(res => {
        if (res.code === 200) {
          this.dialogTableVisible = false
          this.$message({
            message: '关联成功!',
            type: 'success'
          })
          this.getlist()
        } else {
          this.$message({
            message: '关联失败!',
            type: 'error'
          })
        }
      }).catch(err => {
        console.log('err', err)
      })
    },
    // 获取下拉列表
    async getxialalist() {
      try {
        const res = await this.$Http.guanlianitem()
        if (res.code === 200) {
          res.result.forEach((item, index) => {
            this.options[index] = { label: item.indexName, value: item.id }
          })
          console.log('options', this.options)
        }
      } catch (err) {
        console.log('err', err)
      }
    },
    // 关联指标项
    guanlian(row, id) {
      this.dialogTableVisible = true
      this.guanlianid = row.id
    },
    // 搜索
    search() {
      this.$Http.searchitem({
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
        formIndexName: this.input
      }).then(res => {
        if (res.code === 200) {
          this.totalcount = res.result.totalRows
          this.tableData = res.result.resultData
        }
      }).catch(err => {
        console.log('err', err)
      })
    },
    // 删除
    removeTodo(row, index) {
      this.$Http.removeitem(row.id).then(res => {
        if (res.code === 200) {
          this.tableData.splice(index, 1)
          this.$message({
            message: '删除成功!',
            type: 'success'
          })
        }
      }).catch(err => {
        console.log('err', err)
      })
    },
    // 获取列表
    getlist() {
      this.$Http.getkaohelist(
        {
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
          formIndexName: this.input
        }
      ).then(res => {
        if (res.code === 200) {
          this.totalcount = res.result.totalRows
          this.tableData = res.result.resultData
        }
      }).catch(err => {
        console.log('err', err)
      })
    },

    // 跳转在线编辑页面
    goedit(row, index) {
      this.$router.push({
        path: '/xiugaibiaodanxiang',
        query: {
          id: row.id,
          formIndexName: row.formIndexName
        }
      })
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.getlist()
    },
    handleCurrentChange(val) {
      this.currentPage = val

      this.getlist()
    }
  }
}
</script>
<style lang="scss" scoped>
  .centent {
    padding: 25px;

    .ipt_box {
      width: 100%;
      padding: 15px;
      display: flex;
      flex-direction: row;
        justify-content: space-between;
      span{
        text-align: center;
        padding-top: 0.6%;
        margin-left: 2%;
      }
    }
  }
</style>
